#include "include.h"
#include "func.h"
#include "func_bt.h"
#include "api_spiflash_ex.h"
#include "func_exspiflash_music.h"

#define SFX_USE_EXSPI
#define BT_TTS_VOLUME 8
#define TTS_SONG_START_NUM 12

void sbc_decode_exit(void);
void btrf_power_balance_exit(void);

func_bt_t f_bt;
u8 tts_play_flag=0;
u8 bt_audio_disable=0;
u32 music_index=0;

ALIGNED(64)
u16 func_bt_chkclr_warning(u16 bits)
{
    u16 value;
    GLOBAL_INT_DISABLE();
    value = f_bt.warning_status & bits;
    if(value != 0) {
        f_bt.warning_status &= ~value;
        GLOBAL_INT_RESTORE();
        return value;
    }
    GLOBAL_INT_RESTORE();
    return value;
}

AT(.text.func.bt)
void set_timeout_time(void)
{
	f_bt.idle_cnt=0;
	if(sys_cb.mic_onoff)
	{
		f_bt.max_timeout=BT_MICON_TIMEOUT;
	}
	else
	{
		if(!bt_nor_get_link_info(NULL)||bt_get_status() == BT_STA_CONNECTED)
		{
			f_bt.max_timeout=BT_IDLE_TIMEOUT;
		}
		else if(bt_get_status() == BT_STA_PLAYING)
		{
			f_bt.max_timeout=BT_MICON_TIMEOUT;
		}
		else if(bt_get_status() == BT_STA_IDLE||bt_get_status() ==BT_STA_SCANNING)
		{
			if(f_bt.out_f_range)
			{
				f_bt.max_timeout=BT_RECONNNECT_TIMEOUT;
			}
			else
			{
				f_bt.max_timeout=BT_IDLE_TIMEOUT;
			}
		}
		else
		{
			f_bt.max_timeout=BT_IDLE_TIMEOUT;
		}
	}
	printf("bt set time out:%d\n",f_bt.max_timeout);
}

#if FUNC_BT_EN
#if BT_HFP_REC_EN
void bt_sco_rec_start(void);
void bt_sco_rec_stop(void);
void bt_sco_rec_mix_do(u8 *buf, u32 samples);
void bt_sco_rec_init(void);

AT(.com_text.bt_rec)
void bt_sco_rec_mix(u8 *buf, u32 samples)
{
    bt_sco_rec_mix_do(buf, samples);
}

AT(.text.func.bt)
void func_bt_sco_rec_init(void)
{
    rec_src.spr = SPR_8000;
    rec_src.nchannel = 0x01;
    rec_src.source_start = bt_sco_rec_start;
    rec_src.source_stop  = bt_sco_rec_stop;
    f_bt.rec_pause = 0;
    rec_cb.sco_flag = 1;
    bt_sco_rec_init();
}
#endif

#if BT_REC_EN
void bt_music_rec_start(void);
void bt_music_rec_stop(void);

AT(.text.func.bt)
void bt_music_rec_init(void)
{
    rec_src.spr = SPR_44100;
    if (DAC_OUT_SPR == DAC_OUT_48K) {
        rec_src.spr = SPR_48000;
    }
#if KARAOK_REC_EN
    rec_src.nchannel = 0x11;
#else
	rec_src.nchannel = 0x82;
#endif
    rec_src.source_start = bt_music_rec_start;
    rec_src.source_stop  = bt_music_rec_stop;
    f_bt.rec_pause = 0;
    rec_cb.sco_flag = 0;
}

AT(.text.func.bt)
bool bt_rec_status_process(void)
{
    if (func_cb.sta == FUNC_BT) {
        func_bt_status();
        if ((f_bt.disp_status > BT_STA_PLAYING) || (f_bt.disp_status < BT_STA_CONNECTED)) {
            if ((f_bt.disp_status > BT_STA_PLAYING) && rec_cb.sco_flag) {
                return true;
            }
            f_bt.rec_pause = 1;
            return false;       //结束录音
        }
    }
    return true;
}

AT(.text.func.bt)
void bt_music_rec_continue(void)
{
#if !BT_HFP_REC_EN
    if ((f_bt.rec_pause) && ((f_bt.disp_status == BT_STA_PLAYING) || (f_bt.disp_status == BT_STA_CONNECTED))) {
        msg_enqueue(KU_REC);    //继续录音
        f_bt.rec_pause = 0;
    }
#endif
}

#endif // BT_REC_EN
#ifdef SFX_USE_EXSPI
void func_bt_audio_disable(void)
{
	if(bt_audio_disable)
	{
		return;
	}
	bt_audio_disable=1;
	printf("audio bypass!!");
	bt_audio_bypass();
}
void func_bt_audio_enable(void)
{
	if(!bt_audio_disable)
	{
		return;
	}
	bt_audio_disable=0;
	printf("audio enable!!");
	bt_audio_enable();
}

AT(.text.func.bt)
void bt_tts_play(u8 tts_index)
{

	u16 msg;
	if(f_bt.total_music<TTS_SONG_START_NUM)
	{
		music_index=tts_index;
		switch(music_index)
		{
			case BT_CONNECTED_TTS:
				func_mp3_res_play(RES_BUF_CONNECTED_MP3, RES_LEN_CONNECTED_MP3);
				break;
			case BT_DISCONNECT_TTS:
				func_mp3_res_play(RES_BUF_DISCONNECT_MP3, RES_LEN_DISCONNECT_MP3);
				break;
			case BT_RECONNECT_TTS:
				func_mp3_res_play(RES_BUF_EN_RECONNECT_MP3, RES_LEN_EN_RECONNECT_MP3);
				break;
			case BT_PAIR_TTS:
				func_mp3_res_play(RES_BUF_PAIRING_MP3, RES_LEN_PAIRING_MP3);
				break;
			case BT_RESET_TTS:
				func_mp3_res_play(RES_BUF_EN_TIMEOUT_MP3, RES_LEN_EN_TIMEOUT_MP3);
				break;
		}

	}
	else
	{
		music_index=tts_index+TTS_SONG_START_NUM;
		if(f_bt.song_play_flag||tts_play_flag)
		{
			while(get_music_dec_sta() == MUSIC_PLAYING)
			{
				WDT_CLR();
				 msg = msg_dequeue();
				func_message(msg);
			}
		}
		music_control(MUSIC_MSG_STOP);
		func_bt_audio_disable();
		amp_speaker_unmute();
		if(!tts_play_flag&&(!f_bt.song_play_flag))
		{

			//bt_audio_bypass();
			exspiflash_init();
		}
		bsp_change_volume(BT_TTS_VOLUME);
		printf("music_index:%d\n",music_index);
		exspifalsh_music_num_kick(music_index);
		delay_ms(100);
		f_bt.song_play_flag=0;
		tts_play_flag=1;
	}


}
#endif

AT(.text.func.bt)
void func_bt_close_music_control(void)
{
	#ifdef SFX_USE_EXSPI
	if(tts_play_flag)
	{
		tts_play_flag=0;
		printf("music close bbbb!!\n");
		music_control(MUSIC_MSG_STOP);
		if(f_bt.disp_status != BT_STA_PLAYING)
		{
			amp_speaker_mute();
		}
		func_bt_audio_enable();
		//bt_audio_enable();

	}
	#endif
}
void func_bt_mp3_res_play(u32 addr, u32 len)
{
    if (len == 0) {
        return;
    }

#if BT_TWS_EN
    bool tws_res_is_busy(void);
    while(tws_res_is_busy()) {
        tws_res_proc();
    }
#endif

#if BT_REC_EN
    sfunc_record_pause();
#endif
	//func_bt_close_music_control();
    bt_audio_bypass();
    mp3_res_play(addr, len);
    bt_audio_enable();
#if BT_REC_EN
    sfunc_record_continue();
#endif
}

//切换提示音语言
void func_bt_switch_voice_lang(void)
{
#if (LANG_SELECT == LANG_EN_ZH)
    if (xcfg_cb.lang_id >= LANG_EN_ZH) {
        sys_cb.lang_id = (sys_cb.lang_id) ? 0 : 1;
        multi_lang_init(sys_cb.lang_id);
        param_lang_id_write();
        param_sync();
        if (xcfg_cb.bt_tws_en) {
            bt_tws_sync_setting();                                              //同步语言
            tws_res_play(TWS_RES_LANGUAGE_EN + sys_cb.lang_id);                 //同步播放语言提示音
        } else {
            func_mp3_res_play(RES_BUF_LANGUAGE_MP3, RES_LEN_LANGUAGE_MP3);
        }
    }
#endif
}

#if BT_TWS_EN
static void func_bt_tws_set_channel(void)
{
    if(f_bt.tws_status & 0xc0) {   //对箱状态.
        tws_get_lr_channel(f_bt.tws_status);
        dac_mono_init(0, sys_cb.tws_left_channel);
    } else {
        dac_mono_init(1, 0);
    }
}

u8 func_bt_tws_get_channel(void)
{
    return sys_cb.tws_left_channel;
}
#endif

void func_bt_warning(void)
{
    u16 tws_warning;
#if BT_TWS_EN
    bool tws_res_is_busy(void);
    while(tws_res_is_busy()) {
        tws_res_proc();
    }
#endif

    tws_warning = func_bt_chkclr_warning(BT_WARN_TWS_DISCON | BT_WARN_TWS_CON);
    if(tws_warning) {
#if BT_TWS_EN
        if(xcfg_cb.bt_tws_en) {
            if(xcfg_cb.bt_tws_lr_mode != 0) {
                func_bt_tws_set_channel();
            }
        #if WARNING_BT_TWS_DISCON
            if (tws_warning == BT_WARN_TWS_DISCON) {
                func_mp3_res_play(RES_BUF_TWS_DISCON_MP3, RES_LEN_TWS_DISCON_MP3);
            }
        #endif
        }
#endif
    }

    if(func_bt_chkclr_warning(BT_WARN_DISCON)) {
#if WARNING_BT_DISCONNECT
		set_timeout_time();
		if(!f_bt.manu_disconnect)
		{
			bsp_set_led_mode(LED_SEQUENCE_10B);
			bt_set_scan(true,true);
			bt_tts_play(BT_DISCONNECT_TTS);
        	//func_mp3_res_play(RES_BUF_DISCONNECT_MP3, RES_LEN_DISCONNECT_MP3);
		}
		f_bt.manu_disconnect=0;
#endif // WARNING_BT_DISCONNECT
#if WARNING_BT_WAIT_CONNECT
        func_mp3_res_play(RES_BUF_WAIT4CONN_MP3, RES_LEN_WAIT4CONN_MP3);
#endif // WARNING_BT_WAIT_CONNECT
        f_bt.autoplay = 0;
    }

	if(func_bt_chkclr_warning(BT_WARN_CON)) {
#if WARNING_BT_CONNECT
		bsp_set_led_mode(LED_SEQUENCE_10A_1);
		bt_tts_play(BT_CONNECTED_TTS);
        //func_mp3_res_play(RES_BUF_CONNECTED_MP3, RES_LEN_CONNECTED_MP3);
#endif
        f_bt.autoplay = 1;
		set_timeout_time();

    }

#if WARNING_BT_TWS_CONNECT
    if(xcfg_cb.bt_tws_en) {
        tws_warning = func_bt_chkclr_warning(BT_WARN_TWS_SCON | BT_WARN_TWS_MCON );
        if(tws_warning != 0) {
            f_bt.tws_had_pair = 1;
            if (xcfg_cb.bt_tws_lr_mode != 0) {
                func_bt_tws_set_channel();
            }
            ///固定声道方案，TWS连接后异步播放声道提示音。否则同步播放连接提示音
            if (xcfg_cb.bt_tws_lr_mode >= 8) {
                tws_get_lr_channel(tws_warning << 4);

                if(sys_cb.tws_left_channel) {
                    func_mp3_res_play(RES_BUF_LEFT_CH_MP3, RES_LEN_LEFT_CH_MP3);
                } else {
                    bt_audio_bypass();
                    delay_5ms(200);
                    func_mp3_res_play(RES_BUF_RIGHT_CH_MP3, RES_LEN_RIGHT_CH_MP3);
                    bt_audio_enable();
                }
            } else {
                if (tws_warning & BT_WARN_TWS_MCON) {
                    tws_res_play(TWS_RES_TWS_CONNECTED);
                }
            }
        }
    }
#endif
#if WARNING_BT_PAIR
    if(func_bt_chkclr_warning(BT_WARN_PAIRING)) {

		bt_tts_play(BT_PAIR_TTS);
       // func_mp3_res_play(RES_BUF_PAIRING_MP3, RES_LEN_PAIRING_MP3);
    }
#endif
#if BT_HID_MENU_EN
    //按键手动断开HID Profile的提示音
    if (xcfg_cb.bt_hid_menu_en) {
    #if WARNING_BT_HID_MENU
        if (func_bt_chkclr_warning(BT_WARN_HID_CON)) {
            func_mp3_res_play(RES_BUF_CAMERA_ON_MP3, RES_LEN_CAMERA_ON_MP3);
        }
    #endif

    #if WARNING_BT_HID_MENU
        if (func_bt_chkclr_warning(BT_WARN_HID_DISCON)) {
            func_mp3_res_play(RES_BUF_CAMERA_OFF_MP3, RES_LEN_CAMERA_OFF_MP3);
        }
    #endif

    #if BT_HID_DISCON_DEFAULT_EN
        if (f_bt.hid_discon_flag) {
            if (bt_hid_is_ready_to_discon()) {
                f_bt.hid_discon_flag = 0;
                bt_hid_disconnect();
            }
        }
    #endif // BT_HID_DISCON_DEFAULT_EN
    }
#endif // BT_HID_MENU_EN
}
void func_bt_reconnect_tts(void)
{
	//bsp_set_light_mode(LIGHT_SEQUENCE_6A);
	bsp_set_led_mode(LED_SEQUENCE_10A);
	printf("out_f_range 00:%d\n",f_bt.out_f_range);
	if(!f_bt.out_f_range)
	{
		bt_tts_play(BT_RECONNECT_TTS);
	}
}
void func_bt_reconnect_fail_tts(void)
{
	//bsp_set_led_mode(LED_SEQUENCE_10B);
	if(f_bt.manu_disconnect)
	{
		return;
	}
	bt_tts_play(BT_RESET_TTS);
}
void func_bt_into_pair_mode(void)
{
	f_bt.idle_cnt=0;

	bt_disconnect(1);
	bt_set_scan(true,true);
	f_bt.manu_disconnect=1;
	f_bt.pair_timeout_cnt=0;
	f_bt.out_f_range=0;
	f_bt.max_timeout=BT_IDLE_TIMEOUT;
	bsp_set_led_mode(LED_SEQUENCE_10A);
	bt_tts_play(BT_PAIR_TTS);
}

void func_bt_lossconnect_msg(void)
{
	f_bt.out_f_range=1;
	set_timeout_time();
}

void func_bt_disp_status(void)
{
    uint status = bt_get_disp_status();
	uint tmp_status;
    if(f_bt.disp_status != status) {
        f_bt.disp_status = status;
		tmp_status=f_bt.disp_status;
        f_bt.sta_update = 1;
        if(!bt_is_connected()) {
            en_auto_pwroff();
            sys_cb.sleep_en = BT_PAIR_SLEEP_EN;
        } else {
            dis_auto_pwroff();
            sys_cb.sleep_en = 1;
        }
		printf("bt status:%d\n",f_bt.disp_status);
		if((f_bt.disp_status!=BT_STA_PLAYING)&&(tts_play_flag==0))
		{
			amp_speaker_mute();
		}
        switch (f_bt.disp_status) {
        case BT_STA_CONNECTING:
            if (BT_RECONN_LED_EN) {
                led_bt_reconnect();
                break;
            }
        case BT_STA_INITING:
        case BT_STA_IDLE:
            led_bt_idle();
			sys_cb.vol=VOL_MAX;
#if WARNING_BT_PAIR
            if(f_bt.need_pairing && f_bt.disp_status == BT_STA_IDLE) {
                f_bt.need_pairing = 0;
                if(xcfg_cb.warning_bt_pair && xcfg_cb.bt_tws_en) {
                    f_bt.warning_status |= BT_WARN_PAIRING;
                }
            }
#endif
            break;
        case BT_STA_SCANNING:
            led_bt_scan();
			sys_cb.vol=VOL_MAX;
            break;

        case BT_STA_DISCONNECTING:
            led_bt_connected();
			sys_cb.vol=VOL_MAX;
            break;

        case BT_STA_CONNECTED:

			f_bt.manu_disconnect=0;
			f_bt.pair_timeout_cnt=0;
			if(tmp_status>BT_STA_CONNECTED)
			{
                bsp_set_led_mode(LED_SEQUENCE_5);
			}
			set_timeout_time();
            led_bt_connected();
            break;
        case BT_STA_INCOMING:
            led_bt_ring();
            break;
        case BT_STA_PLAYING:
			f_bt.out_f_range=0;
			//func_bt_close_music_control();
			amp_speaker_unmute();
			set_timeout_time();
			bsp_set_led_mode(LED_SEQUENCE_5);
            led_bt_play();
            break;
        case BT_STA_OUTGOING:
        case BT_STA_INCALL:
            led_bt_call();
            break;
        }

        if(f_bt.disp_status >= BT_STA_CONNECTED) {
            f_bt.need_pairing = 1;
        }
#if BT_BACKSTAGE_EN
        if (f_bt.disp_status < BT_STA_PLAYING && func_cb.sta_break != FUNC_NULL) {
            func_cb.sta = func_cb.sta_break;
        }
#endif
    }
}

AT(.text.func.bt)
void func_bt_status(void)
{
    func_bt_disp_status();

#if FUNC_BTHID_EN
    if(is_bthid_mode()) {
        func_bt_hid_warning();
    } else
#endif
    {
        func_bt_warning();
    }
}

AT(.text.func.bt)
void func_bt_sub_process(void)
{
    func_bt_status();
#if USER_TKEY_DEBUG_EN
    bsp_tkey_spp_tx();
#endif
}

AT(.text.func.bt)
void check_song_play(void)
{
	if(!f_bt.song_play_flag&&(!tts_play_flag))
	{
		return;
	}
	if(get_music_dec_sta() != MUSIC_PLAYING)
	{
		printf("music stop!!\n");
		music_control(MUSIC_MSG_STOP);
		f_bt.song_play_flag=0;
		tts_play_flag=0;
		if(!sys_cb.mic_onoff&&f_bt.disp_status != BT_STA_PLAYING)
		{
			amp_speaker_mute();
			bsp_change_volume(sys_cb.vol);
		}
		else
		{
			bsp_change_volume(sys_cb.vol);
		}
		func_bt_audio_enable();
		if(music_index==(BT_RESET_TTS+TTS_SONG_START_NUM))
		{
			bsp_set_led_mode(LED_SEQUENCE_2);
		}
		else if(f_bt.bt_mic_tts)
		{
			f_bt.bt_mic_tts=0;
			if(f_bt.disp_status ==BT_STA_CONNECTED)
			{
				bsp_set_led_mode(LED_SEQUENCE_5);
			}
			else
			{
				bsp_set_led_mode(LED_SEQUENCE_10A);
			}
		}
	}
}
AT(.text.func.bt)
void func_bt_process(void)
{
    func_process();
    func_bt_sub_process();
	//check_song_play();
    if(f_bt.disp_status == BT_STA_INCOMING) {
#if BT_HFP_RING_NUMBER_EN
        sfunc_bt_ring();
#endif
        reset_sleep_delay();
        reset_pwroff_delay();
        f_bt.siri_kl_flag = 0;
        f_bt.user_kl_flag = 0;
#if BT_REC_EN
        bt_music_rec_continue();
#endif
    } else if(f_bt.disp_status == BT_STA_OTA) {

#if SYS_KARAOK_EN
    bsp_karaok_exit(AUDIO_PATH_KARAOK);
#endif

        sfunc_bt_ota();
        reset_sleep_delay();
        reset_pwroff_delay();
    } else if(f_bt.disp_status >= BT_STA_OUTGOING) {
        sfunc_bt_call();
        reset_sleep_delay();
        reset_pwroff_delay();
        f_bt.siri_kl_flag = 0;
        f_bt.user_kl_flag = 0;
#if BT_REC_EN
        bt_music_rec_continue();
#endif
    }

    if(sys_cb.pwroff_delay == 0) {
        sys_cb.pwrdwn_tone_en = 1;
		music_control(MUSIC_MSG_STOP);
        func_cb.sta = FUNC_PWROFF;
        return;
    }
    if(sleep_process(bt_is_sleep)) {
        f_bt.disp_status = 0xff;
    }
}

AT(.text.func.bt)
void func_bt_init(void)
{
    if (!f_bt.bt_is_inited) {
        msg_queue_clear();
        bsp_bt_init();
        f_bt.bt_is_inited = 1;
    }
}

AT(.text.func.bt)
void func_bt_chk_off(void)
{
    if ((func_cb.sta != FUNC_BT) && (f_bt.bt_is_inited)) {
#if BT_PWRKEY_5S_DISCOVER_EN
        bsp_bt_pwrkey5s_clr();
#endif
        bt_disconnect(1);
        bt_off();
        f_bt.bt_is_inited = 0;
    }
}
AT(.text.func.bt)
void func_bt_reset(void)
{
	bt_nor_delete_link_info();
	set_timeout_time();
	f_bt.manu_disconnect=1;
	f_bt.pair_timeout_cnt=0;
	bt_set_scan(true,true);
	bsp_set_led_mode(LED_SEQUENCE_4);
	bt_tts_play(BT_RESET_TTS);
	//delay_5ms(40);
	//bsp_set_led_mode(LED_SEQUENCE_2);
}
AT(.text.func.bt)
void func_bt_sync_remote_device_vol(void)
{
	if(bt_get_status() != BT_STA_INCALL)
	{
	        printf("A2DP SET VOL: %d\n", sys_cb.vol);
			if(!tts_play_flag)
			{
				bsp_change_volume(sys_cb.vol);
			}
	        param_sys_vol_write();
	        sys_cb.cm_times = 0;
	        sys_cb.cm_vol_change = 1;
	}
}
AT(.text.func.bt)
void check_bt_pair_timeout(void)
{
	if(!f_bt.manu_disconnect)
	{
		return;
	}
	f_bt.pair_timeout_cnt++;
	printf("pair time cnt:%d\n",f_bt.pair_timeout_cnt);
	if(f_bt.pair_timeout_cnt>=BT_IDLE_TIMEOUT)
	{
		f_bt.manu_disconnect=0;
		f_bt.pair_timeout_cnt=0;
		bt_set_scan(false,true);
		bsp_set_led_mode(LED_SEQUENCE_10B);
		bt_tts_play(BT_RESET_TTS);
		delay_5ms(190);
	}
}
AT(.text.func.bt)
void check_bt_ilde_timeout(void)
{
	check_song_play();
	check_bt_pair_timeout();
	#if 0
	if(sys_cb.mic_onoff)
	{
		f_bt.idle_cnt=0;
		return;
	}
	#endif
	f_bt.idle_cnt++;
	if(f_bt.idle_cnt>1)
	{
		printf("idle cnt:%d\n",f_bt.idle_cnt);
	}
	if(f_bt.disp_status ==BT_STA_CONNECTED&&f_bt.out_f_range)
	{
		if(f_bt.idle_cnt>1)
		{
			f_bt.out_f_range=0;
		}
	}

	if(f_bt.idle_cnt>=f_bt.max_timeout)
	{
		if(f_bt.disp_status ==BT_STA_PLAYING)
		{
			bt_music_play_pause();
			sys_cb.pwrdwn_tone_en=1;
		}
		else
		{
			sys_cb.pwrdwn_tone_en=0;
		}
		music_control(MUSIC_MSG_STOP);
		func_cb.sta=FUNC_PWROFF;
		f_bt.idle_cnt=0;
	}
}
AT(.text.func.bt)
void func_bt_ctr_play(void)
{

	f_bt.idle_cnt=0;
	f_bt.pair_timeout_cnt=0;
	bt_music_play_pause();
}
AT(.text.func.bt)
void func_bt_mic_onoff(u8 onoff)
{
	if(onoff)
	{
		if(tts_play_flag)
		{
			while(get_music_dec_sta() == MUSIC_PLAYING)
			{
				WDT_CLR();
			}
			tts_play_flag=0;
			music_control(MUSIC_MSG_STOP);
		}
		printf("mic on!!\n");
		sys_cb.mic_onoff=1;
		f_bt.idle_cnt=0;
		//bsp_set_led_mode(LED_SEQUENCE_6);
		if(f_bt.disp_status !=BT_STA_PLAYING&&tts_play_flag==0)
		{
			amp_speaker_mute();
		}
		bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
		if(f_bt.disp_status !=BT_STA_PLAYING)
		{
			//delay_ms(300);
			//amp_speaker_unmute();
			f_bt.bt_mic_tts=1;
			bsp_set_led_mode(LED_SEQUENCE_7);
			bt_tts_play(BT_MIC_TTS);
		}
		else
		{
			//bsp_set_led_mode(LED_SEQUENCE_6);
		}
	}
	else
	{
		printf("mic off!!\n");
		sys_cb.mic_onoff=0;
		f_bt.idle_cnt=0;
		if(f_bt.disp_status !=BT_STA_PLAYING&&tts_play_flag==0)
		{
			amp_speaker_mute();
			delay_ms(100);
		}
		bsp_karaok_exit(AUDIO_PATH_KARAOK);
		if(f_bt.disp_status ==BT_STA_PLAYING)
		{
			bsp_set_led_mode(LED_SEQUENCE_3);
			set_timeout_time();
		}
		else
		{
			f_bt.bt_mic_tts=1;
			bsp_set_led_mode(LED_SEQUENCE_8);
			bt_tts_play(BT_MIC_TTS);
		}
		#if 0
		else if(f_bt.disp_status ==BT_STA_CONNECTED)
		{
			bsp_set_led_mode(LED_SEQUENCE_5);
		}
		else
		{
			bsp_set_led_mode(LED_SEQUENCE_10A);
		}
		#endif



	}
}
AT(.text.func.bt)
void func_bt_lower_power_check(void)
{

	if (sys_cb.vbat <3120)
	 {
	 	printf("sys_cb.vbat:%d->cnt:%d\n",sys_cb.vbat,f_bt.lowpower_cnt);
	 	f_bt.lowpower_cnt++;
		if(f_bt.lowpower_cnt>4)
		{
			music_control(MUSIC_MSG_STOP);
			func_cb.sta=FUNC_PWROFF;
		}
	 }
	else
	{
		f_bt.lowpower_cnt=0;
	}
}
void check_exspiflash_tatol_music(void)
{
	register_spi_read_function(spiflash1_read);
	//f_bt.total_music=48;
	#if 1
	exspiflash_init();
	f_bt.total_music=exspifalsh_music_get_file_total();
	printf("exspi total music:%d\n",f_bt.total_music);
	if(f_bt.total_music<TTS_SONG_START_NUM)
	{
		register_spi_read_function(NULL);
	}
	#endif
}
AT(.text.func.bt)
void func_bt_enter(void)
{

    func_cb.mp3_res_play = func_bt_mp3_res_play;

#if WARNING_FUNC_BT
    mp3_res_play(RES_BUF_BT_MODE_MP3, RES_LEN_BT_MODE_MP3);
#endif // WARNING_FUNC_BT

#if WARNING_BT_WAIT_CONNECT
    mp3_res_play(RES_BUF_WAIT4CONN_MP3, RES_LEN_WAIT4CONN_MP3);
#endif // WARNING_BT_WAIT_CONNECT
	bsp_change_volume(0);
	music_control(MUSIC_MSG_STOP);
	tts_play_flag=0;
	#if SYS_KARAOK_EN
	if(sys_cb.mic_onoff)
	{
		//bsp_karaok_exit(AUDIO_PATH_KARAOK);
    	bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
		delay_ms(10);
	}
    #endif
    func_bt_enter_display();
    led_bt_init();
    dis_auto_pwroff();
    func_bt_init();
	f_bt.song_play_flag=0;
	tts_play_flag=0;
	check_exspiflash_tatol_music();
	amp_speaker_unmute();
	bsp_change_volume(VOL_MAX);
	f_bt.pair_timeout_cnt=0;
	if(!bt_nor_get_link_info(NULL)||sys_cb.bt_mode==1)
	{
		bt_set_scan(true,true);
		f_bt.manu_disconnect=1;
		bt_audio_disable=1;
		bsp_set_led_mode(LED_SEQUENCE_10A);
		bt_tts_play(BT_PAIR_TTS);

	}
	else
	{
		bt_set_scan(false,true);
		f_bt.manu_disconnect=0;
		bt_audio_disable=1;
		func_bt_audio_enable();
	}

	sys_cb.bt_mode=0;
    f_bt.disp_status = 0xfe;
    f_bt.rec_pause = 0;
    f_bt.pp_2_unmute = 0;
    sys_cb.key2unmute_cnt = 0;
	f_bt.lowpower_cnt=0;
	sys_cb.bt_connected=0;
	set_timeout_time();
    bt_redial_init();

    //bt_audio_enable();
#if DAC_DNR_EN
    dac_dnr_set_sta(1);
    sys_cb.dnr_sta = 1;
#endif

#if BT_PWRKEY_5S_DISCOVER_EN
    if(bsp_bt_pwrkey5s_check()) {
        f_bt.need_pairing = 0;  //已经播报了
        func_bt_disp_status();
#if WARNING_BT_PAIR
        func_mp3_res_play(RES_BUF_PAIRING_MP3, RES_LEN_PAIRING_MP3);
#endif
    } else {
        func_bt_disp_status();
#if WARNING_BT_PAIR
        if (xcfg_cb.warning_bt_pair && !xcfg_cb.bt_tws_en) {
            func_mp3_res_play(RES_BUF_PAIRING_MP3, RES_LEN_PAIRING_MP3);
        }
#endif // WARNING_BT_PAIR
    }
#endif

#if BT_REC_EN
    bt_music_rec_init();
#endif // BT_REC_EN

#if BT_TWS_EN
    if(xcfg_cb.bt_tws_pair_mode > 1) {
        bt_tws_set_scan(1, 1);
    }
#endif

#if BT_POWER_OPTIMIZE
    vddio_voltage_configure();  //该函数会回调getcfg_vddio_sel,在蓝牙下把VDDIO设置为2.7V左右
#endif
#if SYS_KARAOK_EN
    if (f_bt.disp_status < BT_STA_INCOMING) {
		#if SYS_KARAOK_EN
		if(sys_cb.mic_onoff)
		{
			 //bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
		}
		#endif
    }
#endif

}

AT(.text.func.bt)
void func_bt_exit(void)
{
#if BT_REC_EN
    sfunc_record_stop();
#endif // BT_REC_EN

    dac_fade_out();
#if DAC_DNR_EN
    dac_dnr_set_sta(0);
    sys_cb.dnr_sta = 0;
#endif
#if BT_PWRKEY_5S_DISCOVER_EN
    bsp_bt_pwrkey5s_clr();
#endif
    func_bt_exit_display();
   // bt_audio_bypass();
	func_bt_audio_disable();
	music_control(MUSIC_MSG_STOP);
	if(sys_cb.mic_onoff)
	{
		 bsp_karaok_exit(AUDIO_PATH_KARAOK);
		 delay_ms(30);
	}
	register_spi_read_function(NULL);
#if BT_TWS_EN
    dac_mono_init(1, 0);
#endif
#if !BT_BACKSTAGE_EN
    bt_disconnect(1);
    bt_off();
    f_bt.bt_is_inited = 0;
#else
    if (bt_get_status() == BT_STA_PLAYING && !bt_is_testmode()) {        //蓝牙退出停掉音乐
        delay_5ms(10);
        if(bt_get_status() == BT_STA_PLAYING) {     //再次确认play状态
            u32 timeout = 850; //8.5s
            bt_music_pause();
            while (bt_get_status() == BT_STA_PLAYING && timeout > 0) {
                timeout--;
                delay_5ms(2);
            }
        }
    }
#endif

#if BT_RF_POWER_BALANCE_EN
    btrf_power_balance_exit();
#endif
    f_bt.rec_pause = 0;
    f_bt.pp_2_unmute = 0;
    sys_cb.key2unmute_cnt = 0;
	tts_play_flag=0;
#if BT_POWER_OPTIMIZE
    vddio_voltage_configure();  //该函数会回调getcfg_vddio_sel,退出蓝牙时，恢复正常配置中的VDDIO电压
#endif

    func_cb.last = FUNC_BT;
}

AT(.text.func.bt)
void func_bt(void)
{
    printf("%s\n", __func__);

    func_bt_enter();

    while (func_cb.sta == FUNC_BT) {
        func_bt_process();
        func_bt_message(msg_dequeue());
        func_bt_display();
    }

    func_bt_exit();
}
#if 0
#define test_put    0
#if test_put

#define SCLK_GPIOCLR    GPIOBCLR
#define SCLK_GPIOSET    GPIOBSET
#define SCLK_BIT        BIT(1)

#define SDAT_GPIOCLR    GPIOBCLR
#define SDAT_GPIOSET    GPIOBSET
#define SDAT_BIT        BIT(0)
void my_spi_init_rec(void)
{
    GPIOBDE |=BIT(0)|BIT(1)|BIT(2);
    GPIOBFEN &= ~(BIT(0)|BIT(1)|BIT(2));
    GPIOBDIR &= ~(BIT(0)|BIT(1)|BIT(2));
    GPIOBSET = BIT(0)|BIT(1)|BIT(2);

//    GPIOADE |=BIT(3)|BIT(4);
//    GPIOAFEN &= ~(BIT(3)|BIT(4));
//    GPIOADIR &= ~(BIT(3)|BIT(4));
//    GPIOASET = BIT(3)|BIT(4);

//    GPIOFDE  |= SDAT_BIT|SCLK_BIT;
//    GPIOFFEN &= ~(SDAT_BIT|SCLK_BIT);
//    GPIOFDIR &= ~(SDAT_BIT|SCLK_BIT);
//    GPIOFSET  = SDAT_BIT|SCLK_BIT;
}
AT(.com_text.putc)
void my_spi_putc_rec(u8 ch)
{
    GLOBAL_INT_DISABLE();
    u8 i;
    for(i=0; i<8; i++) {
        SCLK_GPIOCLR = SCLK_BIT;
//        GPIOBCLR = BIT(0);
        if(ch & 0x80) {
            SDAT_GPIOSET = SDAT_BIT;
//            GPIOBSET = BIT(1);
        } else {
            SDAT_GPIOCLR = SDAT_BIT;
//            GPIOBCLR = BIT(1);
        }
        ch <<= 1;
        SCLK_GPIOSET = SCLK_BIT;
//        GPIOBSET = BIT(0);
    }
    GLOBAL_INT_RESTORE();
}
AT(.com_text.dac)
void put_spi_buf(u8 *ptr, u16 len)
{
    GPIOBCLR = BIT(2);

    for (u16 i = 0; i < len; i++) {
        my_spi_putc_rec(ptr[i]);
    }

    GPIOBSET = BIT(2);
}
#else
void my_spi_init_rec(void){}
void put_spi_buf(u8 *ptr, u16 len){}
#endif

//AT(.com_text.dac)
//void dac_src0_dma_out_proc(void *buf, uint len)           //用户定义DMA输出（使用DMA则不用one byte发送）
//{
    //put_spi_buf(buf, 128);
//}
#endif // 0
#endif //FUNC_BT_EN
